package org.jeecg.modules.zhima.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.jeecg.modules.zhima.admin.entity.ZmGamePlayer;
import org.jeecg.modules.zhima.admin.entity.ZmPlayer;
import org.jeecg.modules.zhima.admin.service.IZmGamePlayerService;
import org.jeecg.modules.zhima.admin.service.IZmPlayerService;
import org.jeecg.modules.zhima.portal.domain.GamePlayer;
import org.jeecg.modules.zhima.portal.service.IGamePlayerService;
import org.jeecg.modules.zhima.portal.service.IPayOrderService;
import org.jeecg.modules.zhima.portal.service.IPlayerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class GamePlayerServiceImpl implements IGamePlayerService {
    @Autowired
    private IZmGamePlayerService zmGamePlayerService;
    @Autowired
    private IZmPlayerService zmPlayerService;
    @Autowired
    private IPlayerService playerService;
    @Autowired
    private IPayOrderService payOrderService;

    @Override
    public ZmGamePlayer editPlayerScore(GamePlayer gamePlayer) {
        ZmGamePlayer old = zmGamePlayerService.getById(gamePlayer.getId());
        if (old == null || ObjectUtils.isEmpty(gamePlayer.getScoreStr())){
            return null;
        }
        old.setScore((long) (Double.parseDouble(gamePlayer.getScoreStr()) * 100));
        zmGamePlayerService.updateById(old);
        return old;
    }

    @Override
    public GamePlayer getOnePlayerId(String gameId, String playerId) {
        ZmGamePlayer zmGamePlayer = zmGamePlayerService.getOne(new LambdaQueryWrapper<ZmGamePlayer>().eq(ZmGamePlayer::getGameId, gameId).eq(ZmGamePlayer::getPlayerId, playerId));
        return fill(zmGamePlayer);
    }

    @Override
    public ZmGamePlayer getOneByUserId(String gameId, String userId) {
        ZmPlayer zmPlayer = playerService.getByUserId(userId);
        return zmGamePlayerService.getOne(new LambdaQueryWrapper<ZmGamePlayer>().eq(ZmGamePlayer::getGameId, gameId).eq(ZmGamePlayer::getPlayerId, zmPlayer.getId()));
    }

    @Override
    public List<GamePlayer> listJoiners(String gameId) {
        List<GamePlayer> gamePlayers = new ArrayList<>();
        List<ZmGamePlayer> zmGamePlayerList = zmGamePlayerService.list(new LambdaQueryWrapper<ZmGamePlayer>().eq(ZmGamePlayer::getGameId, gameId).orderByDesc(ZmGamePlayer::getScore));
        for (ZmGamePlayer zmGamePlayer: zmGamePlayerList) {
            boolean flag = payOrderService.checkPayStatusRemote(zmGamePlayer.getId());
            if (flag){//支付成功的记录
//                GamePlayer gamePlayer = new GamePlayer();
//                BeanUtils.copyProperties(zmGamePlayer, gamePlayer);
//                ZmPlayer zmPlayer = zmPlayerService.getById(gamePlayer.getPlayerId());
//                gamePlayer.setPlayer(zmPlayer);
//                DecimalFormat df = new DecimalFormat("######0.00");
//                gamePlayer.setScoreStr(df.format((double)gamePlayer.getScore()/100));
                gamePlayers.add(this.fill(zmGamePlayer));
            }
        }
        return gamePlayers;
    }

    @Override
    public boolean removeById(String id) {
        return zmGamePlayerService.removeById(id);
    }

    private GamePlayer fill(ZmGamePlayer zmGamePlayer){
        if (ObjectUtils.isEmpty(zmGamePlayer)){
            return null;
        }
        GamePlayer gamePlayer = new GamePlayer();
        BeanUtils.copyProperties(zmGamePlayer, gamePlayer);
        ZmPlayer zmPlayer = zmPlayerService.getById(gamePlayer.getPlayerId());
        gamePlayer.setPlayer(zmPlayer);
        DecimalFormat df = new DecimalFormat("######0.00");
        gamePlayer.setScoreStr(df.format((double)gamePlayer.getScore()/100));
        return gamePlayer;
    }
}
